package myoa

import myoa.domain.RoleName
import myoa.domain.UserStatus
import myoa.tool.ShiroTool

class User {
    String name      //as login
    String password
    Date lastLogin
    UserStatus status

    String fullName
    String tel
    String email
    static hasMany = [roles: Role, permissions: String]

    static constraints = {
        name(unique: true, blank: false) //MySQL数据库中，默认unique index是不区分大小写的
        password(blank: false)           //登录名不区分大小写，密码因为经过了加密计算，是区分大小写的
        fullName(unique: true, nullable: true)
        tel(unique: true, nullable: true)
        email(unique: true, nullable: true)
    }

    boolean passwordEq(String passwordOpen) {
        ShiroTool.encrypt(passwordOpen) == password
    }

    void encryptPassword(String passwordOpen) {
        password = ShiroTool.encrypt(passwordOpen)
    }

    void encryptPassword() {
        encryptPassword(password)
    }

    boolean hasRole(RoleName roleName) {
        def find = roles.find {
            it.name == roleName.name()
        }
        return find != null
    }
}
